package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.entity.User;
import com.qf.service.UserService;
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.PrivateKeyResolver;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/05/17:07
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/userList")
    public List<User> getUserList(){
        List<User> list = userService.list();
        return list;
    }
    @RequestMapping("/addUser")
    public boolean addUser(@RequestBody User user){
        user.setCreateTime(new Date());
       return userService.save(user);
    }
    @RequestMapping(value = "/delUser/{id}")
    public boolean delUser(@PathVariable(value = "id") Integer id){
        System.out.println(id);
        boolean b = userService.removeById(id);
        return b;
    }
    @RequestMapping("queryUser")
    public List<User> queryUserList(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user.getUsername())){
            wrapper.like("username",user.getUsername());
        }
       if(!StringUtils.isEmpty(user.getEmail())){
           wrapper.eq("email",user.getEmail());
       }
     if(!StringUtils.isEmpty(user.getPhone())){
         wrapper.eq("phone",user.getPhone());
     }
     return userService.list(wrapper);
    }
    @RequestMapping(value = "getUserById/{id}")
   public  User getUserById(@PathVariable(value = "id") Integer id){
        User user = userService.getById(id);
        return user;
    }
    @RequestMapping("/updateUser")
    public boolean updatUser(@RequestBody User user){
        System.out.println(user.getId());
        boolean b = userService.updateById(user);
        return b;
    }
    @RequestMapping("/getUserByName")
    User getUserByName(String username){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userService.getOne(wrapper);
        return user;
    }

    @RequestMapping("/getUserByUuid")
    User getUserByUuid(@RequestParam(value = "uuid") String uuid){
        System.out.println(uuid);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("oauth_id",uuid);
        User user = userService.getOne(wrapper);
        return user;
    }

}
